package com.dao.impl;

import com.bean.User;
import com.dao.BaseDao;
import com.dao.UserDao;

public class UserDaoImpl extends BaseDao implements UserDao {
    //邮箱修改
    @Override
    public int updataUser(User user) {
        String sql = "UPDATE user SET  `useremail`= ? where `userName`=?";
        return update(sql, user.getUseremail(), user.getUserName());
    }

    //修改密码
    @Override
    public int updataUserpassword(User user) {
        String sql = "UPDATE user SET  `userpassword`=?   where `userName`=?";
        return update(sql, user.getUserpassword(), user.getUserName());
    }

    //按用户名查询用户
    @Override
    public User queryUserByUsername(String userName) {
        String sql = "SELECT `id`, `userName`, `userpassword`, `useremail`, `userphone`, `userstatus` ,`userpic` FROM `user` WHERE `userName` =  ?";
        return queryForOne(User.class, sql, userName);
    }

    //根据邮箱密码验证
    @Override
    public User queryUserByUsernameemail(String userName, String useremail) {
        String sql = "SELECT `id`, `userName`, `userpassword`, `useremail`, `userphone`, `userstatus` ,`userpic` FROM `user` WHERE `userName` =  ? or `useremail`=?";
        return queryForOne(User.class, sql, userName, useremail);
    }

    //通过用户名和密码查询用户
    @Override
    public User queryUserByUsernameAndPassword(String userName, String userpassword) {
        String sql = "SELECT `id`, `userName`, `userpassword`, `useremail` , `userphone`, `userstatus` ,`userpic`  FROM `user` WHERE `userName` = ? AND `userpassword` = ?";
        return queryForOne(User.class, sql, userName, userpassword);

    }

    //添加保存用户
    @Override
    public int saveUser(User user) {
        String sql = "INSERT INTO `user`(`id`,`userName`, `userpassword`, `useremail` , `userphone`, `userstatus` ,`userpic` ) VALUES (?,?,?,?,?,?,?)";
        return update(sql, user.getId(), user.getUserName(), user.getUserpassword(), user.getUseremail(), user.getUserphone(), user.getUserstatus(), user.getUserpic());
    }

    /**
     * 根据邮箱查询用户
     *
     * @param useremail
     * @return
     */
    @Override
    public User queryUserByUseremail(String useremail) {
        String sql = "SELECT `id`, `userName`, `userpassword`, `useremail` , `userphone`, `userstatus` ,`userpic`  FROM `user` WHERE `useremail` = ? ";
        return queryForOne(User.class, sql, useremail);
    }
}
